//
//  XUMessegeCell.m
//  新浪微博
//
//  Created by xuwenxin on 15-4-15.
//  Copyright (c) 2015年 xuwenxin. All rights reserved.
//

#import "XUMessegeCell.h"
#import "HMUser.h"
#import "XUMessegeModel.h"
#import "XUPhotoCell.h"
#import "HMPhoto.h"
#import "XUPhotoCollectionView.h"

@interface XUMessegeCell () <UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *textName;

@property (weak, nonatomic) IBOutlet UITextView *textViewMessege;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHigh;

@property (nonatomic, strong) NSArray *photos;
@end

@implementation XUMessegeCell

- (void)setStatus:(XUMessegeModel *)status
{
    _status = status;
    HMUser *user = status.user;
    NSURL *url = [NSURL URLWithString:user.profile_image_url];
    [self.iconImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"icon.jpg"]];
    self.textName.text = user.name;
    
    // 取出微博内容进行图文混排
    NSString *messege = status.text;
    self.textViewMessege.attributedText = [self weiboMessege:messege];
    
    self.timeLabel.text = status.created_at;
    self.photos = status.pic_urls;
    [self.collectionView reloadData];
//    [self updateConstraints];
}

// 进行图文混排
- (NSAttributedString *)weiboMessege:(NSString *)messege
{

    // 利用正则表达式找出需要替换的图和文
    
    // 保存为一个可变数组(图,文,图,文....)
    
    // 将所有图转为需要显示的图
    
    // 重新生成一个数组字符串
    
    // 遍历数组取出所有字符串拼接成一个属性字符串
    
    // 返回这个属性字符串
    
    
    return nil;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    self.collectionViewHigh.constant = 100;
//    if (self.photos.count == 0) {
//        self.collectionViewHigh.constant = 0;
//    }
    return self.photos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     XUPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
     HMPhoto *model =[HMPhoto objectWithKeyValues:self.photos[indexPath.item]];
    cell.photoURL = model;
        return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([_delegate respondsToSelector:@selector(XUMessegeCellDelegateWithPhoto:index:)]) {
        [_delegate XUMessegeCellDelegateWithPhoto:self.photos index:indexPath];
    }
    
}
- (void)awakeFromNib {
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
